% by YLX,clq on 6/16
% Version 2.0
%% 画图部分需要改进
%% 几乎全同于 figure10
%% for fig 11 13 15

function [prctages, cost_struct, profits_prct] = figure1x_odd(j_all_all)

    global I J ci cI r CURRENT s_time S

    ci_origin = ci;
    cI_origin = cI;
    r_origin = r;

    ci = 40;
    r = 100;
    cIs = 100:20:200;
    N = size(j_all_all, 2);
    len_seq = size(j_all_all, 1); %认为j_all_all为seq行向量的组合
    len_cIs = size(cIs, 2);
    index = zeros(1,len_seq); % 每个seq最优利润病人的总数
    cost_struct = zeros(3,I); %收入减去两个成本的矩阵
    opt_profits = zeros(1,N);  %同个seq的最高利润组合
    opt_costruct = zeros(3,N);  %所对应的cost structure
    max_profits = zeros(1,len_seq); %不同seq的最高利润
    max_costructs = zeros(3,len_seq); % 所对应的cost structure
    profits_prct = zeros(3,len_cIs); % percentiles 对应的平均利润
    costructure = zeros(3,len_cIs);
    i_star = zeros(1,N);
    slot_patientno = zeros(I,len_seq);
    prctages = zeros(I, len_cIs);

    for jj = 1:len_cIs
        cI = cIs(jj);
        for ii = 1:len_seq
            fprintf("\t fig1x_odd -> %d/%d * %d/%d\n", jj, len_cIs, ii, len_seq);
            if mod(ii, 10) == 0
                fprintf("\t\t Totle time = %.1f\n", ftimer()-s_time);
            end 

            j_all = j_all_all(ii, :);
            [best_is, opt_profits, EYI, EYin, opt_costruct,place] = prop_policy2(j_all, N);
            %记录每个j_all利润最高时的利润，利润part，slot分配情况
            max_profits(1,ii) = opt_profits(place);%记录利润
            max_costructs(:,ii)=opt_costruct(:,place);%记录利润组成
            slot_patientno(:,ii)=sum(S,2);%记录slot分配
        end

    prctages(:,jj) = sum(slot_patientno, 2)/sum(sum(slot_patientno));
    profits_prct(:, jj) = prctile(max_profits,[10; 50; 90]);
    costructure(:, jj) = mean(max_costructs,2);
    end

    figure(11)
    for i = 1:I
        if mod(i,2)==0
            plot(cIs, 100*prctages(i,:),'--',"LineWidth",2);
        else
            plot(cIs, 100*prctages(i,:),"LineWidth",2);
        end
        hold on
    end
    xlabel("c_I");
    ylabel("Percentage of patients assigned");
    yticks([6:2:18]);
    xticks([100:20:200]);
    legend('1','2','3','4','5','6','7','8','Location','Best')
    text(195,13,'\downarrow 2-7')
    text(195,17,'\uparrow 1')
    text(195,8,'\downarrow 8')
    ylim([6 18]);
    grid on

    % Fig 13
    figure(13)
    for i = 1:3
        plot(cIs, profits_prct(i, :),"LineWidth",2);
        hold on
    end
    xlabel("c_I");
    ylabel("Expected Profit");
    yticks([1000:50:1450]);
    xticks([100:10:200]);
    legend("10 prctile","50 prctile" ,"90 prctile","location","Best");
    ylim([1000 1450]);
    grid on
    %input("Press ENTER to continue.\n");
    
    
    % Fig 15
    figure(15)
    for i = 1:3
        plot(cIs,costructure(i,:),"LineWidth",2);
        hold on
    end
    xlabel("c_I");
    ylabel("Components of expected profit for various c_I");
    yticks([0:500:2500]);
    xticks([100:10:200]);
    legend("r E[\Sigma_iX_i]","c_i E[\Sigma_iY_i]" ,"c_I E[\Sigma_IY_I]","location","Best");
    ylim([0 2500])
    grid on
    %input("Press ENTER to continue.\n")
    % <<< 

    % >>> 恢复global变量的值（如果后续没用到原来的值，不写也行）
    ci = ci_origin;
    cI = cI_origin;
    r = r_origin;
    % <<<
    return;


